package dao.impl;

import dao.OrderDao;
import model.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Order dao.
 *
 * @author gzp
 * @create 2022 -12-12 10:04
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * The Pstmt.
     */
    PreparedStatement pstmt = null;
    /**
     * The Rs.
     */
    ResultSet rs = null;

    @Override
    public int insert(Order order) {
        int rows = 0;
        String sql = "INSERT INTO orders VALUES(null,?,?,?,?,?)";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, order.getUid());
            pstmt.setLong(2, order.getSid());
            pstmt.setDate(3, order.getDate());
            pstmt.setLong(4, order.getState());
            pstmt.setString(5, order.getTradeNo());
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int delete(int oid) {
        int rows = 0;
        String sql = "DELETE FROM orders WHERE oid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, oid);
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public Order selectByOid(long oid) {
        Order order = null;
        String sql = "SELECT * FROM orders WHERE oid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            pstmt.setLong(1, oid);
            while (rs.next()) {
                order = new Order();
                order.setOid(rs.getLong("oid"));
                order.setUid(rs.getLong("uid"));
                order.setSid(rs.getLong("sid"));
                order.setDate(rs.getDate("date"));
                order.setTradeNo(rs.getString("tradeno"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return order;
    }

    @Override
    public List<Order> selectByUid(long uid, int pageIndex, int pageSize) {
        List<Order> orderList = new ArrayList<Order>();
        String sql = "SELECT * FROM orders where uid=? AND state=1 LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, uid);
            pstmt.setInt(2, (pageIndex - 1) * pageSize);
            pstmt.setInt(3, pageSize);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOid(rs.getLong("oid"));
                order.setUid(rs.getLong("uid"));
                order.setSid(rs.getLong("sid"));
                order.setDate(rs.getDate("date"));
                order.setTradeNo(rs.getString("tradeno"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return orderList;
    }

    @Override
    public List<Order> selectBySid(long sid, int pageIndex, int pageSize) {
        List<Order> orderList = new ArrayList<Order>();
        String sql = "SELECT * FROM orders where sid=? LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, sid);
            pstmt.setInt(2, (pageIndex - 1) * pageSize);
            pstmt.setInt(3, pageSize);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOid(rs.getLong("oid"));
                order.setUid(rs.getLong("uid"));
                order.setSid(rs.getLong("sid"));
                order.setDate(rs.getDate("date"));
                order.setTradeNo(rs.getString("tradeno"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return orderList;
    }

    @Override
    public List<Order> selectAll(int pageIndex, int pageSize) {
        List<Order> orderList = new ArrayList<Order>();
        String sql = "SELECT * FROM orders LIMIT ?,?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, (pageIndex - 1) * pageSize);
            pstmt.setInt(2, pageSize);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Order order = new Order();
                order.setOid(rs.getLong("oid"));
                order.setUid(rs.getLong("uid"));
                order.setSid(rs.getLong("sid"));
                order.setDate(rs.getDate("date"));
                order.setTradeNo(rs.getString("tradeno"));
                orderList.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return orderList;
    }

    @Override
    public int update(String tradeNo) {
        int rows = 0;
        String sql = "UPDATE orders SET state=1 WHERE tradeNo=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, tradeNo);
            rows = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, pstmt, conn);
        }
        return rows;
    }

    @Override
    public int countAll() {
        int count = 0;
        String sql = "SELECT count(*) FROM orders";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }

    @Override
    public int countBySid(long sid) {
        int count = 0;
        String sql = "SELECT count(*) FROM orders WHERE sid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, sid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }

    @Override
    public int countByUid(long uid) {
        int count = 0;
        String sql = "SELECT count(*) FROM orders WHERE uid=?";
        Connection conn = BaseDao.getConnection();
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1, uid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(rs, pstmt, conn);
        }
        return count;
    }
}
